<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/2/15
 * Time: 15:21
 */

namespace app\admin\model;


use think\Db;

class GivePresen extends DataBase
{
    protected $name = 'give_presen';

    public $prizeType = [1=>'金币','钻石',3=>'座驾','礼物',5=>'喇叭','生肖卡'];
    //public $prizeType = [4=>'座驾',6=>'喇叭'];

    public function getDataList()
    {

        $page_config = [
            'page'=>input('offset',1),
            'list_rows'=>input('limit',10),
        ];

        $key = input('get.key');
        $value = input('get.value');
        $where = [];

        if($key == 'u_id' && $value){
            $user_id = User::where('u_id',$value)->value('id');
            $where[] = ['user_id','eq',$user_id];
        }
        if($key == 'account' && $value)
            $where[] = ['remark','like','%'.$value.'%'];

        if($key == 'admin' && $value)
            $where[] = ['remark','like','%'.$value.'%'];

        // $order = input('sort','create_time').' '.input('order','DESC');

        $data = $this->where($where)->where('status','neq',0)->order('id desc')->paginate($page_config);

        // dump($data);

        foreach($data as $key=>$value){

            $userInfo = User::where('id','=',$value['user_id'])->field('u_id,nickname')->find();

            $data[$key]['u_id'] = $userInfo['u_id'];
            $data[$key]['nickname'] = $userInfo['nickname'];
            $type = $value['prize_type'];
            $data[$key]['prize_type'] = $this->prizeType[$type];
            switch($type){
                case 1: $data[$key]['gift_name'] = '金币'; break;
                case 2: $data[$key]['gift_name'] = '钻石'; break;
                case 3:
                    $data[$key]['gift_name'] = Vehicle::where('vehicle_id','=',$value['prize_id'])->value('vehicle_name');
                    break;
                case 4:
                    $data[$key]['gift_name'] = Gift::where('id','=',$value['prize_id'])->value('gift_name');
                    break;
                case 5:
                    $data[$key]['gift_name'] = ShopHorn::where('horn_id','=',$value['prize_id'])->value('horn_name');
                    break;
                case 6:
                    $data[$key]['gift_name'] = Card::where('id','=',$value['prize_id'])->value('name');
                    break;

                default: $data[$key]['gift_name'] = '未知';

            }
            $data[$key]['prize_number'] = $value['prize_number'];
            $data[$key]['status'] = $value['status'];
            $data[$key]['approv_time'] = $value['approv_time'];

            $pos = strpos($data[$key]['remark'],'%');
            if($pos){
                $arr = explode("%",$data[$key]['remark']);
                $data[$key]['account'] = $arr[0];
                $data[$key]['content'] =$arr[1];
                $data[$key]['remark'] = $arr[2] ? $arr[2] : '活动获取';
            }else{
                $data[$key]['account'] = '2019/4/19前未知';
                $data[$key]['content'] = '2019/4/19前未知';
                $data[$key]['remark'] = $value['remark']?$value['remark']:'活动获取';
            }
            $data[$key]['create_time'] =date('Y-m-d H:i:s',$value['create_time']);

        }
        //  dump($data);
        return $data;
    }
    public function getApproveList()
    {

        $page_config = [
            'page'=>input('offset',1),
            'list_rows'=>input('limit',10),
        ];

        $key = input('get.key');
        $value = input('get.value');
        $where = [];

        if($key == 'u_id' && $value){
            $user_id = User::where('u_id',$value)->value('id');
            $where[] = ['user_id','eq',$user_id];
        }
        if($key == 'account' && $value)
            $where[] = ['remark','like','%'.$value.'%'];

        if($key == 'admin' && $value)
            $where[] = ['remark','like','%'.$value.'%'];

        // $order = input('sort','create_time').' '.input('order','DESC');

        $data = $this->where($where)->where('status','eq',0)->group('create_time')->order('id desc')->paginate($page_config);


        foreach($data as $key=>$value){

            $userInfo = User::where('id','=',$value['user_id'])->field('u_id,nickname')->find();

            $data[$key]['u_id'] = $userInfo['u_id'];
            $data[$key]['nickname'] = $userInfo['nickname'];
            $type = $value['prize_type'];
            $data[$key]['prize_type'] = $this->prizeType[$type];
            switch($type){
                case 1: $data[$key]['gift_name'] = '金币'; break;
                case 2: $data[$key]['gift_name'] = '钻石'; break;
                case 3:
                    $data[$key]['gift_name'] = Vehicle::where('vehicle_id','=',$value['prize_id'])->value('vehicle_name');
                    break;
                case 4:
                    $data[$key]['gift_name'] = Gift::where('id','=',$value['prize_id'])->value('gift_name');
                    break;
                case 5:
                    $data[$key]['gift_name'] = ShopHorn::where('horn_id','=',$value['prize_id'])->value('horn_name');
                    break;
                case 6:
                    $data[$key]['gift_name'] = Card::where('id','=',$value['prize_id'])->value('name');
                    break;

                default: $data[$key]['gift_name'] = '未知';

            }
            $data[$key]['prize_number'] = $value['prize_number'];

            $pos = strpos($data[$key]['remark'],'%');
            if($pos){
                $arr = explode("%",$data[$key]['remark']);
                $data[$key]['account'] = $arr[0];
                $data[$key]['content'] =$arr[1];
                $data[$key]['remark'] = $arr[2] ? $arr[2] : '活动获取';
            }else{
                $data[$key]['account'] = '2019/4/19前未知';
                $data[$key]['content'] = '2019/4/19前未知';
                $data[$key]['remark'] = $value['remark']?$value['remark']:'活动获取';
            }
            $data[$key]['create_time'] =date('Y-m-d H:i:s',$value['create_time']);

        }
        //  dump($data);
        return $data;
    }

    /**
     * @return array
     * @date 2019/2/18
     * @user 帅帅吴
     * @title 二级联动选择礼物
     */
    public function giftName()
    {
        $prize_type = input('prize_type');

        if(!$prize_type) return ['code'=>-100,'msg'=>'数据异常'];

        switch ($prize_type){
            case 1 : $data = ''; break;
            case 2 : $data = ''; break;
            case 3 :
                $Info = Vehicle::field('vehicle_id,vehicle_name')->select();
                foreach($Info as $key => $value){
                    $data[$key]['id'] = $value['vehicle_id'];
                    $data[$key]['name'] = $value['vehicle_name'];
                }; break;
            case 4 :
//                $Info = Gift::field('id,gift_name,diamonds')->order('diamonds DESC')->select();
//                $Info = Gift::field('id,gift_name')->order('diamonds DESC')->select();
                $Info = Gift::field('id,gift_name')->order('id DESC')->select();
                foreach($Info as $key => $value){
                    $data[$key]['id'] = $value['id'];
                    $data[$key]['name'] = $value['gift_name'];
//                    $data[$key]['diamonds'] = $value['diamonds'];
                    $data[$key]['diamonds'] = 0;
                }; break;
            case 5 :
                $Info = ShopHorn::field('horn_id,horn_name')->select();
                foreach($Info as $key => $value){
                    $data[$key]['id'] = $value['horn_id'];
                    $data[$key]['name'] = $value['horn_name'];
                }; break;
            case 6 :
                $data = Card::field('id,name')->select();break;

            case 7:
                $Info = Prop::field('id,prop_name')->select();
                foreach($Info as $key => $value){
                    $data[$key]['id'] = $value['id'];
                    $data[$key]['name'] = $value['prop_name'];
                }; break;
            default : $data = '';
        }

        return ['code'=>100,'data'=>$data];
    }


    public function givePresenAdd()
    {
        $u_id = (int)input('user_id');
        $newData['user_id'] = User::where('u_id','=',$u_id)->value('id');
        $newData['prize_number'] = (int)input('prize_number');
        $newData['prize_type'] = intval(input('prize_type'));



        $newData['prize_id'] = (int)input('prize_id');

        $account =input('account');
        $content =trim(input('content'));
        if(!$account || !$content) return ['code'=>-100,'msg'=>'赠送理由或备注未填'];

        $newData['remark'] = $account.'%'.$content.'%官方赠送，操作员:'.session('admin_name');
        $newData['create_time'] = time();
        $newData['status'] = 1;
        $newData['approv_time'] = time();

        if(in_array($newData['prize_type'],[1,2,4]) && session('admin_id') != '23'){
            return ['code'=>-100,'msg'=>'您没有权限'];
        }

        if(!in_array($newData['prize_type'],[1,2]) && empty($newData['prize_id'])) return ['code'=>-100,'msg'=>'请选择礼物'];



        Db::Connect('db_data')->startTrans();
        try{
            $this->save($newData);
            switch ($newData['prize_type']){
                case 1 : User::where('id',$newData['user_id'])->setInc('gold',$newData['prize_number']); $mark='个金币'; break;  //金币
                case 2 : User::where('id',$newData['user_id'])->setInc('diamonds',$newData['prize_number']); $mark='个钻石'; break; //钻石
                case 3 :  //座驾
                    $where[] = ['product_id','=',$newData['prize_id']];
                    $where[] = ['user_id','=',$newData['user_id']];
                    $where[] = ['type','=',1];
                    $where[] = ['expire_time','>',time()];

                    $data['product_id'] = $newData['prize_id'];
                    $data['user_id'] = $newData['user_id'];
                    $data['type'] = 1;
                    $data['number'] = 1;
                    $data['create_time'] = time();
                    $data['expire_time'] = time() + $newData['prize_number'] * 24 * 3600;

                    //  $vehicle_name = Vehicle::where('vehicle_id','=',$newData['prize_id'])->value('vehicle_name');
                    $res = ShopOrder::where($where)->find();

                    if($res) ShopOrder::where('shop_order_id', $res['shop_order_id'])->update(['expire_time' => $res['expire_time'] +$newData['prize_number'] * 24 * 3600 ]);
                    else     ShopOrder::create($data);
                    //   $mark='天【'.$vehicle_name.'】座驾';
                    break;

                case 4 :  //礼物
                    $where[] = ['user_id','=',$newData['user_id']];
                    $where[] = ['gift_id','=',$newData['prize_id']];
                    $res = UserGiftStock::where($where)->find();
                    //$gift_name = GiftUrl::where('id','=',$newData['prize_id'])->value('gift_name');
                    if($res)
                        UserGiftStock::where('id',$res['id'])->update(['gift_num'=>$res['gift_num'] + $newData['prize_number']]);
                    else
                        UserGiftStock::create(['user_id'=>$newData['user_id'],'gift_id'=>$newData['prize_id'],'gift_num'=>$newData['prize_number']]);
                    //  $mark = '个【'.$gift_name.'】礼物';
                    break;

                case 5 : //喇叭
                    $where[] = ['product_id','=',$newData['prize_id']];
                    $where[] = ['user_id','=',$newData['user_id']];
                    $where[] = ['type','=',2];

                    $res = ShopOrder::where($where)->find();
                    // $shop_horn = ShopHorn::where('horn_id','=',$newData['prize_id'])->value('horn_name');
                    if($res)
                        ShopOrder::where('shop_order_id',$res['shop_order_id'])->update(['number'=>$res['number'] + $newData['prize_number']]);
                    else
                        ShopOrder::create(['user_id'=>$newData['user_id'],'product_id'=>$newData['prize_id'],'type'=>2,'number'=>$newData['prize_number'],'create_time'=>time()]);
                    // $mark = '个【'.$shop_horn.'】';
                    break;

                case 6 : //生肖卡
                    $where[] = ['card_id','=',$newData['prize_id']];
                    $where[] = ['user_id','=',$newData['user_id']];
                    $where[] = ['status','=',0];

                    $res = CardUserStock::where($where)->find();
                    //$card_name = Card::where('id','=',$newData['prize_id'])->value('name');
                    if($res)
                        CardUserStock::where($where)->update(['card_num'=>$res['card_num'] + $newData['prize_number'] ]);
                    else
                        CardUserStock::create(['user_id'=>$newData['user_id'],'card_id'=>$newData['prize_id'],'card_num'=>$newData['prize_number'],'status'=>0]);
                    //$mark = '个【'.$card_name.'】';
                    break;
            }
            if($newData['prize_type'] == 2)
                DiamondsAccount($newData['user_id'],1,17,$newData['prize_number'],$mark);
            Db::Connect('db_data')->commit();
            return ['code'=>100,'msg'=>'赠送成功'];
        }catch(\Exception $e){
            Db::Connect('db_data')->rollback();
            return ['code'=>-100,'msg'=>'操作失败'.$e->getMessage()];
        }




    }

    public function largess()
    {
        $u_id = (int)(input('user_id'));
        $newData['user_id'] =  User::where('u_id','=',$u_id)->value('id');
        $newData['prize_type'] = intval(input('a1'));
        $newData['prize_number'] = (int)input('a2');
        $newData['prize_id'] = (int)input('prize_id');
        $content =trim(input('content'));
        $account =input('account');
        $newData['remark'] = $account.'%'.$content.'%官方赠送，操作员:'.session('admin_name');
        $newData['create_time'] = time();

        $newData2['user_id'] =  $newData['user_id'];
        $newData2['prize_type'] = intval(input('a3'));
        $newData2['prize_number'] = (int)input('a4');
        $newData2['prize_id'] = (int)input('prize_id');
        $content =trim(input('content'));
        $account =input('account');
        $newData2['remark'] = $account.'%'.$content.'%官方赠送，操作员:'.session('admin_name');
        $newData2['create_time'] = time();

        $list = [
            $newData,
            $newData2
        ];

        // 启动事务
        $this->startTrans();
        try {

            $this->saveAll($list);


            $this->commit();
            return ['code'=>100,'msg'=>'提交成功,审核中'];
        }catch(\Exception $e){
            $this->rollback();
            return ['code'=>-100,'msg'=>'赠送失败'.$e->getMessage()];
        }

    }


    public function approveCheck()
    {
        $ct = intval(input('get.ct'));
        $status = intval(input('get.status'));
        $lst = $this->where('create_time','eq',$ct)->select();

        // 启动事务
        $this->startTrans();
        try {
            foreach ($lst as $k => $vv){

                $this->update(['status'=>$status,'approv_time'=>time()],['id'=>$vv['id']]);
                if($status == 1){
                    $newData = $this->findOrFail($vv['id']);
                    switch ($newData['prize_type']){
                        case 1 : User::where('id',$newData['user_id'])->setInc('gold',$newData['prize_number']); $mark='个金币'; break;  //金币
                        case 2 : User::where('id',$newData['user_id'])->setInc('diamonds',$newData['prize_number']); $mark='个钻石'; break; //钻石
                        case 3 :  //座驾
                            $where[] = ['product_id','=',$newData['prize_id']];
                            $where[] = ['user_id','=',$newData['user_id']];
                            $where[] = ['type','=',1];
                            $where[] = ['expire_time','>',time()];

                            $data['product_id'] = $newData['prize_id'];
                            $data['user_id'] = $newData['user_id'];
                            $data['type'] = 1;
                            $data['number'] = 1;
                            $data['create_time'] = time();
                            $data['expire_time'] = time() + $newData['prize_number'] * 24 * 3600;

                            //  $vehicle_name = Vehicle::where('vehicle_id','=',$newData['prize_id'])->value('vehicle_name');
                            $res = ShopOrder::where($where)->find();

                            if($res) ShopOrder::where('shop_order_id', $res['shop_order_id'])->update(['expire_time' => $res['expire_time'] +$newData['prize_number'] * 24 * 3600 ]);
                            else     ShopOrder::create($data);
                            //   $mark='天【'.$vehicle_name.'】座驾';
                            break;

                        case 4 :  //礼物
                            $where[] = ['user_id','=',$newData['user_id']];
                            $where[] = ['gift_id','=',$newData['prize_id']];
                            $res = UserGiftStock::where($where)->find();
                            //$gift_name = GiftUrl::where('id','=',$newData['prize_id'])->value('gift_name');
                            if($res)
                                UserGiftStock::where('id',$res['id'])->update(['gift_num'=>$res['gift_num'] + $newData['prize_number']]);
                            else
                                UserGiftStock::create(['user_id'=>$newData['user_id'],'gift_id'=>$newData['prize_id'],'gift_num'=>$newData['prize_number']]);
                            //  $mark = '个【'.$gift_name.'】礼物';
                            break;

                        case 5 : //喇叭
                            $where[] = ['product_id','=',$newData['prize_id']];
                            $where[] = ['user_id','=',$newData['user_id']];
                            $where[] = ['type','=',2];

                            $res = ShopOrder::where($where)->find();
                            // $shop_horn = ShopHorn::where('horn_id','=',$newData['prize_id'])->value('horn_name');
                            if($res)
                                ShopOrder::where('shop_order_id',$res['shop_order_id'])->update(['number'=>$res['number'] + $newData['prize_number']]);
                            else
                                ShopOrder::create(['user_id'=>$newData['user_id'],'product_id'=>$newData['prize_id'],'type'=>2,'number'=>$newData['prize_number'],'create_time'=>time()]);
                            // $mark = '个【'.$shop_horn.'】';
                            break;

                        case 6 : //生肖卡
                            $where[] = ['card_id','=',$newData['prize_id']];
                            $where[] = ['user_id','=',$newData['user_id']];
                            $where[] = ['status','=',0];

                            $res = CardUserStock::where($where)->find();
                            //$card_name = Card::where('id','=',$newData['prize_id'])->value('name');
                            if($res)
                                CardUserStock::where($where)->update(['card_num'=>$res['card_num'] + $newData['prize_number'] ]);
                            else
                                CardUserStock::create(['user_id'=>$newData['user_id'],'card_id'=>$newData['prize_id'],'card_num'=>$newData['prize_number'],'status'=>0]);
                            //$mark = '个【'.$card_name.'】';
                            break;
                    }
                    if($newData['prize_type'] == 2)
                        DiamondsAccount($newData['user_id'],1,17,$newData['prize_number'],$mark);
                }
            }



            // 提交事务
            $this->commit();
            return true;
        } catch (\Exception $e) {
            // 回滚事务
            $this->rollback();
            return false;
        }
    }



}